package cn.edu.nju.askinginhand.activity;

import static cn.edu.nju.askinginhand.constant.QuesType.audio_class1;
import static cn.edu.nju.askinginhand.constant.QuesType.audio_class2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import cn.edu.nju.askinginhand.bean.UserInfo;

/**
 * 
 * @author ZhangZu
 * 
 */
public class AudioQuesListActivity extends Activity {

	private Button quesBtn = null;
	private Spinner firstSpinner = null;
	private Spinner secondSpinner = null;
	private ListView quesList = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.audio_ques_list);

		quesBtn = (Button) findViewById(R.id.quesBtn);
		firstSpinner = (Spinner) findViewById(R.id.firstSpinner);
		secondSpinner = (Spinner) findViewById(R.id.secondSpinner);
		quesList = (ListView) findViewById(R.id.quesList);

		initQuesBtn();
		initSpinner();
		initQuesList();
	}

	public void initQuesBtn() {
		quesBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(AudioQuesListActivity.this,
						AudioQuesActivity.class));
			}
		});
	}

	public void initSpinner() {
		ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
				R.layout.questype_spinner_item, R.id.spinnerText, audio_class1);

		firstSpinner.setAdapter(adapter1);
		firstSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long id) {
				ArrayAdapter<String> adapter2 = null;
				if (position == 0) {
					secondSpinner.setVisibility(View.INVISIBLE);
				} else {
					secondSpinner.setVisibility(View.VISIBLE);
					adapter2 = new ArrayAdapter<String>(
							AudioQuesListActivity.this,
							R.layout.questype_spinner_item, R.id.spinnerText,
							audio_class2[position]);
					secondSpinner.setAdapter(adapter2);
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				secondSpinner.setVisibility(View.INVISIBLE);
			}
		});
	}

	public void initQuesList() {
		SimpleAdapter adapter = new SimpleAdapter(this, getData(),
				R.layout.ques_list_item, new String[] { "typeImage",
						"timeText", "titleText" }, new int[] { R.id.typeImage,
						R.id.timeText, R.id.titleText });
		quesList.setAdapter(adapter);
	}

	private List<Map<String, Object>> getData() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 14; i >= 0; i--) {
			Map<String, Object> news = new HashMap<String, Object>();
			news.put("typeImage", R.drawable.q);
			news.put("timeText", i + "");
			news.put("titleText", "问题" + i);
			list.add(news);
		}
		return list;
	}
}
